<?php
class controller_advs extends model_advs{
    //region 广告分类管理
    //处理广告分类列表参数
    private function category_get_args($get = true){
        $para = $get ? $_GET : $_POST;
        $args = array(
            'sKey' => $this->existsKeyInArray('sKey',$para,'S'),
            'categoryid' => $this->existsKeyInArray('categoryid',$para,'N'),
            'ps' => $this->existsKeyInArray('ps',$para,'N',$this->pagesize),
        );
        return $args;
    }
    //处理广告分类分页参数
    private function category_page_args($args){
        $page = array();
        if(!empty($args['sKey'])) $page['sKey'] = $args['sKey'];
        if(!empty($args['ps'])) $page['ps'] = $args['ps'];
        $page['page'] = '{page}';
        return $page;
    }
	//广告分类
	public function category(){
        $args = $this->category_get_args(true);
        $page_args = $this->category_page_args($args);
        $page_current = $this->get_page();
        $args['page_current'] = $page_current;
        $datas = $this->mod_advs_category($args);
        $args['page_max'] = $this->get_maxpage($args['ps'],$datas['record']);
		$this->display("advs/category",array(
		    'basepath' => '/advs/category',
		    'datas' => $datas['datas'],
            'record' => $datas['record'],
            'page_max' => $args['page_max'],
            'page_current' => $args['page_current'],
            'page_url' => http_build_query($page_args),
            'extra' => '',
            'searchdata' => $args,
            'container' => '#iframe'
        ));
	}
	//处理广告分类添加、编辑表参数
    private function category_add_edit_args(){
        if(!$this->post) core::__403();
        $args = array(
            'id' => $this->existsKeyInArray('id',$_POST,'N'),
            'title' => $this->existsKeyInArray('title',$_POST,'S'),
            'width' => $this->existsKeyInArray('width',$_POST,'N'),
            'height' => $this->existsKeyInArray('height',$_POST,'N'),
        );
        if(empty($args['title'])) core::error('分类名称不能为空');
        return $args;
    }
    //删除单个广告分类
    public function category_delete(){
        $id = $this->existsKeyInArray('id',$_GET,'N');
        if(!$id) core::__403();
        if($id>10){
        $deleteid = $this->db->delete('@@adv_category',array('id'=>$id));
        if(!$deleteid) core::error('删除失败');
            $this->addLog($this->authid,__FUNCTION__,$id,'');
        $this->dialogue(array(
            'type' => 'success',
            'text' => "删除分类成功",
            'auto' => true,
            'yes'  => array(
                'text' => '我知道了',
                'func' => 'JumpMainFrame("/advs/category");',
            ),
        ));
    }else {
            core::error('系统广告分类禁止删除');
        }
    }
	//添加广告分类
    public function category_add(){
        $basepath = '/advs/category';
	    $html = '<div id="mainbody" class="main">
            <form name="add_save" action="'.$basepath.'_add_save"  method="POST" target="ajax" func="form_submit">
            <div class="info">
                <dl><dt>标题：</dt><dd><input type="text" name="title" class="int" /></dd> </dl>
                <dl><dt>长宽：</dt><dd><input type="number" name="width" class="mint" /></dd><dd> X </dd><dd><input type="number" name="height" class="mint" /></dd> </dl>
            </div>
            </form>
        </div>';
        $this->dialogue(array(
            'class' => '',
            'body' => $html,
            'yes'  => array(
                'text' => '保存',
                'func' => '$("form[name=add_save]").submit();',
            ),
            'no'  => array(
                'text' => '关闭',
            ),
        ));
    }
    //编辑广告分类
    public function category_edit(){
        $id = $this->existsKeyInArray('id',$_GET,'N');
        if(!$id) core::__403();
        $data = $this->mod_advs_category_item($id);
        $basepath = '/advs/category';
        $html = '<div id="mainbody" class="main">
            <form name="add_save" action="'.$basepath.'_edit_save"  method="POST" target="ajax" func="form_submit">
            <input name="id" type="hidden" value="'.$data['id'].'" />
            <div class="info">
                <dl><dt>标题：</dt><dd><input type="text" name="title" class="int" value="'.$data['title'].'" /></dd> </dl>
                <dl><dt>长宽：</dt><dd><input type="number" name="width" class="mint" value="'.$data['width'].'" /></dd><dd> X </dd><dd><input type="number" name="height" class="mint" value="'.$data['height'].'" /></dd> </dl>
            </div>
            </form>
        </div>';
        $this->dialogue(array(
            'class' => '',
            'body' => $html,
            'yes'  => array(
                'text' => '保存',
                'func' => '$("form[name=add_save]").submit();',
            ),
            'no'  => array(
                'text' => '关闭',
            ),
        ));
    }
    //添加广告分类-保存
    public function category_add_save(){
        $args = $this->category_add_edit_args();
        unset($args['id']);
        $insertid = $this->table_row_save('@@adv_category',$args);
        if(!$insertid) core::error("添加[".$args['title']."]失败");
        $this->addLog($this->authid,__FUNCTION__,$insertid);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "添加[".$args['title']."]成功",
            'auto' => true,
            'yes'  => array(
                'text' => '刷新',
                'func' => 'JumpMainFrame("/advs/category");',
            ),
            'no'  => array(
                'text' => '返回',
            ),
        ));

    }
    //编辑广告分类-保存
    public function category_edit_save(){
        $args = $this->category_add_edit_args();
        $id = intval($args['id']);
        unset($args['id']);
        $updateid = $this->table_row_save('@@adv_category',$args,array('id'=>$id),true);
        if(!$updateid) core::error("更新[".$args['title']."]失败");
        $this->addLog($this->authid,__FUNCTION__,$id);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "更新[".$args['title']."]成功",
            'auto' => true,
            'yes'  => array(
                'text' => '刷新',
                'func' => 'JumpMainFrame("/advs/category");',
            ),
            'no'  => array(
                'text' => '返回',
            ),
        ));

    }
    //endregion

    //region 广告管理
    //处理广告列表参数
    private function index_get_args($get = true){
        $para = $get ? $_GET : $_POST;
        $args = array(
            'sKey' => $this->existsKeyInArray('sKey',$para,'S'),
            'categoryid' => $this->existsKeyInArray('categoryid',$para,'N'),
            'ps' => $this->existsKeyInArray('ps',$para,'N',$this->pagesize),
        );
        return $args;
    }
    //处理广告分页参数
    private function index_page_args($args){
        $page = array();
        if(!empty($args['sKey'])) $page['sKey'] = $args['sKey'];
        if(!empty($args['categoryid'])) $page['categoryid'] = $args['categoryid'];
        if(!empty($args['ps'])) $page['ps'] = $args['ps'];
        $page['page'] = '{page}';
        return $page;
    }
    //广告
    public function index(){
        $args = $this->index_get_args(true);
        $page_args = $this->index_page_args($args);
        $page_current = $this->get_page();
        $args['page_current'] = $page_current;
        $datas = $this->mod_advs_items($args);
        $args['page_max'] = $this->get_maxpage($args['ps'],$datas['record']);
        $category = array();
        $_arr = $this->mod_advs_category_list();
        foreach ($_arr as $v){
            $category[$v['id']] = $v['title'];
        }
        //$category = array_column($category, 'title');
        $this->display("advs/index",array(
            'basepath' => '/advs/index',
            'datas' => $datas['datas'],
            'record' => $datas['record'],
            'page_max' => $args['page_max'],
            'page_current' => $args['page_current'],
            'page_url' => http_build_query($page_args),
            'extra' => '',
            'category' => $category,
            'searchdata' => $args,
            'container' => '#iframe'
        ));
    }
    //处理广告添加、编辑表参数
    private function index_add_edit_args(){
        if(!$this->post) core::__403();
        $args = array(
            'id' => $this->existsKeyInArray('id',$_POST,'N'),
            'title' => $this->existsKeyInArray('title',$_POST,'S'),
            'pic' => $this->existsKeyInArray('pic',$_POST,'S'),
            'categoryid' => $this->existsKeyInArray('categoryid',$_POST,'N'),
            'groupname' => $this->existsKeyInArray('groupname',$_POST,'S'),
            'url' => $this->existsKeyInArray('url',$_POST,'S'),
            'state' => $this->existsKeyInArray('state',$_POST,'N'),
            'actiontime' => $this->existsKeyInArray('actiontime',$_POST,'S'),
            'endtime' => $this->existsKeyInArray('endtime',$_POST,'S'),
            'sort' => $this->existsKeyInArray('sort',$_POST,'N'),
        );
        if(empty($args['title'])) core::error('名称不能为空');
        if(empty($args['categoryid'])) core::error('请选择广告分类');
        return $args;
    }
    //删除单个广告
    public function index_delete(){
        $id = $this->existsKeyInArray('id',$_GET,'N');
        if(!$id) core::__403();
        $deleteid = $this->db->delete('@@advs', array('id' => $id));
        if (!$deleteid) core::error('删除失败');
        $this->addLog($this->authid,__FUNCTION__,$id,'');
        $this->dialogue(array(
            'type' => 'success',
            'text' => "删除成功",
            'auto' => true,
            'yes' => array(
                'text' => '我知道了',
                'func' => 'JumpMainFrame("/advs/index");',
            ),
        ));

    }
    //添加广告
    public function index_add(){
        $_cid = $this->existsKeyInArray('categoryid',$_GET,'N');
        $category = array();
        $_arr = $this->mod_advs_category_list();
        foreach ($_arr as $v){
            $category[$v['id']] = $v['title'];
        }
        $this->display('advs/index_add',array(
           'basepath' => '/advs/index',
            'category' => $category,
            'maxsort' => $this->table_maxsort('@@advs','sort','categoryid='.$_cid),
            'page_current' => $this->existsKeyInArray('ps',$_GET,'N',1),
            'page_url' => http_build_query($_GET),
        ));

    }
    //编辑广告
    public function index_edit(){
        $id = $this->existsKeyInArray('id',$_GET,'N');
        if(!$id) core::__403();
        $data = $this->mod_advs_index_item($id);
        $category = array();
        $_arr = $this->mod_advs_category_list();
        foreach ($_arr as $v){
            $category[$v['id']] = $v['title'];
        }
        $data['actiontime'] = date('Y-m-d H:i:s',$data['actiontime']);
        $this->display('advs/index_edit',array(
            'basepath' => '/advs/index',
            'category' => $category,
            'data' => $data,
            'page_current' => $this->existsKeyInArray('ps',$_GET,'N',1),
            'page_url' => http_build_query($_GET),
        ));
    }
    //添加广告-保存
    public function index_add_save(){
        $args = $this->index_add_edit_args();
        unset($args['id']);
        $args['actiontime'] = !is_numeric($args['actiontime']) ? strtotime($args['actiontime']) : $args['actiontime'];
        $insertid = $this->table_row_save('@@advs',$args);
        if(!$insertid) core::error("添加[".$args['title']."]失败");
        $this->addLog($this->authid,__FUNCTION__,$insertid,$args['title']);
        $this->dialogue(array(
            'type' => 'success',
            'text' => "添加[".$args['title']."]成功",
            'auto' => true,
            'yes'  => array(
                'text' => '刷新',
                'func' => 'JumpMainFrame("/advs/index?categoryid='.$args['categoryid'].'");',
            ),
            'no'  => array(
                'text' => '返回',
            ),
        ));

    }
    //编辑广告-保存
    public function index_edit_save(){
        $args = $this->index_add_edit_args();
        $id = intval($args['id']);
        unset($args['id']);
        $args['actiontime'] = !is_numeric($args['actiontime']) ? strtotime($args['actiontime']) : $args['actiontime'];
        $updateid = $this->table_row_save('@@advs',$args,array('id'=>$id),true);
        if(!$updateid) core::error("更新[".$args['title']."]失败");
        $this->addLog($this->authid,__FUNCTION__,$id,'');
        $this->dialogue(array(
            'type' => 'success',
            'text' => "更新[".$args['title']."]成功",
            'auto' => true,
            'yes'  => array(
                'text' => '刷新',
                'func' => 'JumpMainFrame("/advs/index?categoryid='.$args['categoryid'].'");',
            ),
            'no'  => array(
                'text' => '返回',
            ),
        ));

    }

    //endregion
}